#include <vector>
using namespace std;
/*
 * @lc app=leetcode.cn id=169 lang=cpp
 *
 * [169] 多数元素
 */

// @lc code=start
class Solution {
public:
    // 选举算法, 多数选举
    int majorityElement(vector<int>& nums) {
        int count = 0, mag = -1;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] == mag) count++;
            else if (--count < 1) {
                mag = nums[i]; count = 1;
            } 
        }
        return mag;
    }
};
// @lc code=end

